﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace Heuristic
{
    public partial class MainForm : Form
    {
        PerformanceCounter cpuPerformance;
        System.Windows.Forms.Timer cpuUpdateTimer;

        public MainForm()
        {
            InitializeComponent();

            /* Setup variable */

            // Counter cpu performance
            cpuPerformance = new PerformanceCounter();
            cpuPerformance.CategoryName = "Processor";
            cpuPerformance.CounterName = "% Processor Time";
            cpuPerformance.InstanceName = "_Total";
            
            //
            cpuUpdateTimer = new System.Windows.Forms.Timer();
            cpuUpdateTimer.Interval = Properties.Settings.Default.CpuUpdateTime;
            cpuUpdateTimer.Tick += new EventHandler(cpuUpdateTimer_Tick);
            cpuUpdateTimer.Start();


            /* Set defaut value for windows control */

            cpuStatus.Text = "";


            /* Start update thread */

            /* Application */

        }

        #region Event

        private void Form1_Load(object sender, EventArgs e)
        {
            // Force load first page
            tabControl_SelectedIndexChanged(null, null);
        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            //  abort all callculate
            knight.AbortSimulate();
            queen.AbortSimulate();
            haNoi.AbortSimulate();
            taci.AbortSimulate();
        }

        void cpuUpdateTimer_Tick(object sender, EventArgs e)
        {
            this.UpdateCpuState();
            if (tabControl.SelectedTab == queenTab)
            {
                // Update queen tab
                this.UpdateQueenState();
                return;
            }
            if (tabControl.SelectedTab == knightTab)
            {
                this.UpdateKnightState();
                return;
            }
            if (tabControl.SelectedTab == hanoiTab)
            {
                this.UpdateHaNoiState();
                return;
            }
            if (tabControl.SelectedTab == taciTab)
            {
                this.UpdateTaciState();
                return;
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            knight.AbortSimulate();
            queen.AbortSimulate();
            haNoi.AbortSimulate();
            taci.AbortSimulate();
        }

        #endregion // Event

        #region Supporting function

        private void UpdateCpuState()
        {
            cpuStatus.Text = cpuPerformance.NextValue().ToString();
        }

        private void UpdateQueenState()
        {
            if(queen.IsCompleted)
            {
                this.queenSimulationStatus.Text = "Đã hoàn thành"; 
            }
            else
            {
                this.queenSimulationStatus.Text = "Chưa hoàn thành"; 
            }
        }

        private void UpdateKnightState()
        {
            if (knight.IsCompleted)
            {
                this.knightComplete.Text = "Đã hoàn thành";
            }
            else
            {
                this.knightComplete.Text = "Chưa hoàn thành";
            }
        }

        private void UpdateHaNoiState()
        {
            if (haNoi.IsCompleted)
            {
                this.haNoiStatus.Text = "Đã hoàn thành";
            }
            else
            {
                this.haNoiStatus.Text = "Chưa hoàn thành";
            }

            this.haNoiTransferN.Text = haNoi.Times.ToString();
        }

        private void UpdateTaciState()
        {
            if (taci.IsCompleted)
            {
                this.taciStatus.Text = "Đã hoàn thành";
            }
            else
            {
                this.taciStatus.Text = "Chưa hoàn thành";
            }
        }

        #endregion

        #region Eight Queen

        private void queenSpeeder_Scroll(object sender, EventArgs e)
        {
            int tempSleepTime = 1000 - this.queenSpeeder.Value;
            this.queen.Speed = tempSleepTime;
        }

        private void queenStart_Click(object sender, EventArgs e)
        {
            this.queen.StartSimulate();
        }

        private void queenSteep_Click(object sender, EventArgs e)
        {
            this.queen.Step();
        }

        private void queenReset_Click(object sender, EventArgs e)
        {
            this.queen.ResetSimulate();
        }

        private void queenStop_Click(object sender, EventArgs e)
        {
            this.queen.AbortSimulate();
        }

        /// <summary>
        /// Double click, open in new modal dialog.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void eightQueen_DoubleClick(object sender, EventArgs e)
        {
            EightQueenForm queenForm = new EightQueenForm(this.queen.ChessWidth);
            queenForm.StartPosition = FormStartPosition.CenterScreen;
            queenForm.QueenControl.UpdateWindow();
            queenForm.ShowDialog(this);
        }

        private void changeQueens_Click(object sender, EventArgs e)
        {
            int newValue = (int)queenNumbers.Value;
            this.queen.ChangeChessWidthForControl(newValue);
        }

        #endregion

        #region Knight

        private void knightStart_Click(object sender, EventArgs e)
        {
            knight.StartSimulate();
        }

        private void knightReset_Click(object sender, EventArgs e)
        {
            knight.ResetSimulate();
        }

        private void knightSpeeder_Scroll(object sender, EventArgs e)
        {
            int newSleepTime = 1000 - this.knightSpeeder.Value;
            this.knight.Speed = newSleepTime;
        }

        private void knightStep_Click(object sender, EventArgs e)
        {
            this.knight.Step();
        }

        private void knight_MouseClick(object sender, MouseEventArgs e)
        {
            KnightForm knightForm = new KnightForm();
            knightForm.KnightControl.ForceRefresh();
            knightForm.StartPosition = FormStartPosition.CenterScreen;
            knightForm.ShowDialog(this);
        }

        #endregion

        #region HaNoi

        private void hanoiStart_Click(object sender, EventArgs e)
        {
            haNoi.StartSimulate();
        }

        private void haNoiStep_Click(object sender, EventArgs e)
        {
            haNoi.Step();
        }

        private void haNoiStop_Click(object sender, EventArgs e)
        {
            haNoi.StopSimulate();
        }

        private void haNoiReset_Click(object sender, EventArgs e)
        {
            haNoi.ResetSimulate();
        }

        private void haNoiSpeeder_Scroll(object sender, EventArgs e)
        {
            int newSleepTime = 1000 - haNoiSpeeder.Value;
            haNoi.SleepTime = newSleepTime;
        }

        private void changeHaNoi_Click(object sender, EventArgs e)
        {
            int newValue = (int)haNoiNumber.Value;
            this.haNoi.ChangeTowerHeight(newValue);
        }

        private void haNoi_DoubleClick(object sender, EventArgs e)
        {
            HaNoiForm haNoiForm = new HaNoiForm();
            haNoiForm.StartPosition = FormStartPosition.CenterScreen;
            haNoiForm.HaNoi.ForceRefresh();
            haNoiForm.ShowDialog(this);
        }
        #endregion

        #region Taci

        private void taciStart_Click(object sender, EventArgs e)
        {
            taci.StartSimulate();
        }

        private void taciSpeeder_Scroll(object sender, EventArgs e)
        {
            taci.SleepTime = 1000 - taciSpeeder.Value;
        }

        private void taciReset_Click(object sender, EventArgs e)
        {
            taci.ResetSimulate();
        }

        private void taciStop_Click(object sender, EventArgs e)
        {
            taci.StopSimulate();
        }

        private void taciStartSet_Click(object sender, EventArgs e)
        {
            try
            {
                taci.SetStartState(taciStartState.Text);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString(), "Nhập sai trạng thái khởi đầu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("Sửa đổi trạng thái bắt đầu thành công.", "Trạng thái bắt đầu", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void taciStopSet_Click(object sender, EventArgs e)
        {
            try
            {
                taci.SetStopState(taciStopState.Text);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString(), "Nhập sai trạng thái kết thúc", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("Sửa đổi trạng thái kết thúc thành công.", "Trạng thái kết thúc", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void taciStep_Click(object sender, EventArgs e)
        {
            taci.Step();
        }

        private void taci_DoubleClick(object sender, EventArgs e)
        {
            TaciForm taciForm = new TaciForm();
            taciForm.StartPosition = FormStartPosition.CenterScreen;
            taciForm.Taci.ForceRefresh();
            taciForm.ShowDialog(this);
        }
        #endregion
    }
}
